package com.quick.proxy.server;

import cn.hutool.core.util.StrUtil;
import com.quick.proxy.core.ChannelAttribute;
import com.quick.proxy.core.ChannelConstants;
import com.quick.proxy.core.ProxyChannel;
import com.quick.proxy.core.VisitorChannel;
import com.quick.proxy.core.utils.ProxyUtils;
import com.quick.proxy.core.utils.ResponseUtils;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Description: 代理资源
 * Author: 吃素不吃肉
 * Date: 2023/7/5
 *
 * @author 吃素不吃肉
 */
@Slf4j
public class ProxyResources {
    /**
     * 清理代理通道
     *
     * @param channel 通道
     */
    public static void clearProxyResources(Channel channel) {
        String accessKey = channel.attr(ChannelAttribute.ACCESS_KEY_PROXY_CHANNEL).get();
        if (StrUtil.isEmpty(accessKey)) {
            return;
        }
        log.info("[{}]代理客户端断开连接", accessKey);
        //关闭代理通道对应的所有访问者id通道
        Map<String, VisitorChannel> map = channel.attr(ServerChannelAttribute.VISITOR_ID_CHANNEL).get();
        if (!CollectionUtils.isEmpty(map)) {
            for (Map.Entry<String, VisitorChannel> visitorChannel : map.entrySet()) {
                if (visitorChannel.getValue().getVisitorChannel().isOpen()) {
                    visitorChannel.getValue().getVisitorChannel().close();
                    log.info("visitor[{}]关闭访问者通道", visitorChannel.getValue().getVisitorId());
                }
            }
        }
        //关闭访问者通道
        List<Channel> visitorChannels = channel.attr(ChannelAttribute.VISITOR_CHANNEL).get();
        if (!CollectionUtils.isEmpty(visitorChannels)) {
            for (Channel visitorChannel : visitorChannels) {
                int localPort = ProxyUtils.getLocalPort(visitorChannel);
                ChannelConstants.MAPPING_PORT_REAL_PROXY_CLIENT_IP_PORT.remove(localPort);
                if (visitorChannel.isOpen()) {
                    visitorChannel.close();
                }
                log.info("[{}]关闭访问者服务端端口{}", accessKey, localPort);
            }
        }
        //移除accessKey绑定的代理通道
        ChannelConstants.ACCESS_KEY_PROXY_CHANNEL.entrySet().removeIf(stringChannelEntry -> channel.equals(stringChannelEntry.getValue()));
        //移除映射端口绑定的代理通道
        ChannelConstants.MAPPING_PORT_PROXY_CHANNEL.entrySet().removeIf(stringChannelEntry -> channel.equals(stringChannelEntry.getValue()));
    }

    /**
     * 清理访问者通道
     *
     * @param channel 通道
     */
    public static void clearVisitorResources(Channel channel) {
        //获取访问者id
        String visitorId = channel.attr(ServerChannelAttribute.VISITOR_ID).get();
        //移除掉访问者代理通道
        ProxyChannel proxyChannel = channel.attr(ServerChannelAttribute.PROXY_CHANNEL).get();
        proxyChannel.getProxyChannel().attr(ServerChannelAttribute.VISITOR_ID_CHANNEL).get().remove(visitorId);
        //通知代理客户端关闭访问者id对应的真实代理通道
        ResponseUtils.disabledConnect(visitorId, proxyChannel.getProxyChannel());
        log.info("visitorId[{}]访问者连接断开", visitorId);
    }
}
